﻿@page "/JavaTemp"
@using JavaDevToys.Rcl.Service
@using System.Text

<script>
    var require = { paths: { 'vs': 'https://cdn.masastack.com/npm/monaco-editor/0.34.1/min/vs' } };
</script>
<script src="https://cdn.masastack.com/npm/monaco-editor/0.34.1/min/vs/loader.js"></script>
<script src="https://cdn.masastack.com/npm/monaco-editor/0.34.1/min/vs/editor/editor.main.nls.js"></script>
<script src="https://cdn.masastack.com/npm/monaco-editor/0.34.1/min/vs/editor/editor.main.js"></script>
<MDialog @bind-Value="dialog"
Transition="dialog-bottom-transition"
MaxWidth="600">
    <ChildContent>
        <MCard>
            <MToolbar Color="primary"
            Dark>
                提示
            </MToolbar>
            <MCardText>
                <div class="text-h2 pa-12">保存成功!请重启应用或按F5刷新！</div>
            </MCardText>
            <MCardActions Class="justify-end">
                <MButton Text
                OnClick="() => dialog = false">
                    关闭
                </MButton>
            </MCardActions>
        </MCard>
    </ChildContent>
</MDialog>

<h3>Java模板管理</h3>
<MSelect Items="fileList"
@bind-Value="@selectedFileName"
ItemText="u => u"
ItemValue="u => u"
Filled
Label="选择模板"
FullWidth="false"
Dense></MSelect>
<MDivider></MDivider>
<MMonacoEditor Height="@("100%")" InitOptions="InitEditor" @ref="_editor"></MMonacoEditor>
<div Class="d-flex justify-end align-center flex-wrap" Style="gap: 8px;">
    <MButton Color="primary"
    Class="mt-2  ml-9"
    OnClick="SaveTemp">
        保存模板
    </MButton>
</div>
@code {
    private bool dialog=false;
    private string _selectedFileName;
    public string selectedFileName{
        get => _selectedFileName;
        set{
            if (value != _selectedFileName)
            {
                _selectedFileName = value;
                OnSelectedFileChanged(value);
            }
        }
    }
    private IList<string> fileList;
    private MMonacoEditor _editor;

    [Inject]
    public IJavaFileService javaFileService { get; set; }

    private async Task<object> InitEditor()
    {
        object options = new
        {
            value = "",
            language = "java",
            lineNumbers = "on",
            roundedSelection = false,
            scrollBeyondLastLine = false,
            readOnly = false,
            theme = "vs-dark",
        };
        return options;
    }

    protected override void OnInitialized()
    {
        fileList = Directory.GetFiles(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JavaTemp")).Select(s=>Path.GetFileName(s)).ToList();
        base.OnInitialized();
    }

    private async void OnSelectedFileChanged(string fileName){
        var javaCode=javaFileService.ReadFileAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JavaTemp", fileName));
        await _editor.SetValueAsync(javaCode);
        StateHasChanged();
    }

    private async void SaveTemp(){
        string javaCode=await _editor.GetValueAsync();
        if(string.IsNullOrWhiteSpace(javaCode)){
            return;
        }
        if (string.IsNullOrWhiteSpace(selectedFileName)){
            return;
        }
        File.WriteAllText(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "JavaTemp", selectedFileName), javaCode, new UTF8Encoding(false));
        dialog = true;
        StateHasChanged();
    }
}
